package com.hjq.fcloud.widget

import android.content.Context
import android.graphics.Color
import android.graphics.Typeface
import android.text.TextUtils
import android.util.AttributeSet
import android.util.TypedValue
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.widget.HorizontalScrollView
import android.widget.LinearLayout
import android.widget.TextView
import com.hjq.fcloud.R


/**
 * 水平滚动的item view
 */
class HorizontalScrollItemView : HorizontalScrollView {

    var itemMaxLines:Int = 2

    var titleBgColor: Int? = null
    var titleColor: Int? = null
    var titleBold: Boolean? = false
    var titleSize: Float? = null

    var contentBgColor: Int? = null
    var contentColor: Int? = null
    var contentSize: Float?

    var lineVisible: Boolean? = true
    var lineHeight: Int? = null
    var lineColor: Int? = null

    var itemPaddingLeft: Int? = null
    var itemPaddingTop: Int? = null

    //标题样式，作为一个模版，比如宽度
    var mTitleTemplateItems: ArrayList<HorizontalScrollItem>? = null

    constructor(context: Context) : this(context, null)
    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {

        var array = context.obtainStyledAttributes(attrs, R.styleable.HorizontalScrollItemView)
        itemMaxLines = array.getInt(R.styleable.HorizontalScrollItemView_itemMaxLines, 1)

        titleBgColor = array.getColor(R.styleable.HorizontalScrollItemView_titleBgColor, Color.GREEN)
        titleColor = array.getColor(R.styleable.HorizontalScrollItemView_titleColor, Color.BLACK)
        titleBold = array.getBoolean(R.styleable.HorizontalScrollItemView_titleBold, false)
        titleSize = array.getDimension(
            R.styleable.HorizontalScrollItemView_titleSize,
            resources.getDimension(R.dimen.common_text_size)
        )

        contentBgColor = array.getColor(R.styleable.HorizontalScrollItemView_contentBgColor, Color.WHITE)
        contentColor = array.getColor(R.styleable.HorizontalScrollItemView_contentColor, Color.BLACK)
        contentSize = array.getDimension(
            R.styleable.HorizontalScrollItemView_contentSize,
            resources.getDimension(R.dimen.common_text_size)
        )
        lineVisible = array.getBoolean(R.styleable.HorizontalScrollItemView_lineVisible, true)
        lineHeight = array.getDimensionPixelOffset(
            R.styleable.HorizontalScrollItemView_lineHeight, 1
        )
        lineColor = array.getColor(R.styleable.HorizontalScrollItemView_lineColor, Color.parseColor("#f5f5f5"))

        itemPaddingLeft = array.getDimensionPixelOffset(
            R.styleable.HorizontalScrollItemView_itemPaddingLeft, 10
        )
        itemPaddingTop = array.getDimensionPixelOffset(
            R.styleable.HorizontalScrollItemView_itemPaddingTop, 10
        )
        
        array.recycle()
    }

    fun setViewData(list: ArrayList<ArrayList<HorizontalScrollItem>>) {
        if (list != null && list.size > 0) {
            removeAllViews()
            var rootView = LinearLayout(context)
            rootView.orientation = LinearLayout.VERTICAL
            rootView.layoutParams = LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT
            )
            addView(rootView)

            for (pos in 0..list.size - 1) {
                var items = list.get(pos)
                var itemView = buildItemView(items, pos)
                rootView.addView(itemView)
                if(lineVisible == true){
                    var lineView = buildLineView()
                    rootView.addView(lineView)
                }
            }
        }
    }

    private fun buildLineView(): View {
        var view = View(context)
        var layoutParams =
            LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        layoutParams.height = lineHeight!!
        view.layoutParams = layoutParams
        view.setBackgroundColor(lineColor!!)
        return view
    }

    private fun buildItemView(items: ArrayList<HorizontalScrollItem>, pos: Int): View {
        var linearLayout = LinearLayout(context)
        linearLayout.layoutParams =
            LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
        linearLayout.orientation = LinearLayout.HORIZONTAL
        var isTitle = (pos == 0)
        if (isTitle) {
            mTitleTemplateItems = items
            linearLayout.setBackgroundColor(titleBgColor!!)
        } else {
            linearLayout.setBackgroundColor(contentBgColor!!)
        }

        for (index in 0 .. items.size - 1){
            var item = items.get(index)
            var itemWidth = mTitleTemplateItems!!.get(index).itemWidth
            var textView = TextView(context)
            var layoutParams = LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.MATCH_PARENT
            )
            layoutParams.width = itemWidth
            layoutParams.gravity = Gravity.CENTER
            textView.layoutParams = layoutParams
            textView.setPadding(itemPaddingLeft!!, itemPaddingTop!!, itemPaddingLeft!!, itemPaddingTop!!)
            textView.setText(item.text)
            textView.gravity = Gravity.CENTER
            textView.maxLines = itemMaxLines
            textView.ellipsize = TextUtils.TruncateAt.END
            if(isTitle){
                textView.setTextSize(TypedValue.COMPLEX_UNIT_PX,titleSize!!)
                if(titleBold == true){
                    textView.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.BOLD))
                }else{
                    textView.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.NORMAL))
                }
                textView.setTextColor(titleColor!!)
            }else{
                textView.setTextSize(TypedValue.COMPLEX_UNIT_PX,contentSize!!)
                textView.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.NORMAL))
                textView.setTextColor(contentColor!!)
            }
            linearLayout.addView(textView)
        }

        return linearLayout
    }

}

class HorizontalScrollItem{
    var text:String = ""
    var itemWidth:Int = 0

    constructor(text: String) {
        this.text = text
    }

    constructor(text: String, itemWidth: Int) {
        this.text = text
        this.itemWidth = itemWidth
    }

}